 rem we have used variables a,b,c,d,s,t,i,q,r
   set legacy 0.99
   set tv ntsc
   set romsize 4k
   include div_mul.asm

   player0:
 %00011000
 %00111100
 %11111111
 %01111110
 %00111100
 %00111100
 %00011000
 %00000000
end

   data playlengths
   61
end

   data play1
 248,0,0,128,
 190,0,0,128,
 191,3,0,128,
 255,31,0,128,
 191,63,0,160,
 191,31,0,160,
 255,3,0,240,
 190,0,0,188,
 188,0,0,191,
 248,0,7,255,
 176,0,31,191,
 160,0,127,191,
 224,0,31,255,
 160,0,7,191,
 184,0,1,191,
 255,0,0,254,
 191,3,0,184,
 191,7,0,176,
 255,7,0,224,
 191,3,0,160,
 191,0,0,176,
 252,0,0,254,
 184,0,1,191,
 176,0,7,191,
 224,0,15,255,
 160,0,63,191,
 176,0,31,191,
 248,0,15,255,
 188,0,7,191,
 190,0,1,191,
 224,0,0,228,
 136,64,0,132,
 136,160,16,128,
 160,16,144,161,
 130,8,64,129,
 130,4,32,128,
 128,2,16,128,
 160,1,8,160,
 161,0,4,160,
 130,0,2,160,
 132,0,5,160,
 132,0,8,129,
 132,0,17,129,
 134,0,34,129,
 133,0,68,129,
 130,1,136,161,
 129,130,17,160,
 160,69,34,160,
 160,42,68,160,
 160,252,248,160,
 128,0,0,128,
 160,64,0,160,
 128,160,64,128,
 128,64,128,128,
 128,128,0,128,
 160,0,0,160,
 161,10,0,160,
 128,5,0,128,
 160,2,168,160,
 160,0,80,160,
 224,0,32,224,
 160,0,0,160
end

   sdata mymusic=a
   1,2,3,4,5,6,7,255
end
start
 playfield:
 X..............................X
 X..XXXX.XXX...X...XXXX.XXXX....X
 X..X....X.X..X.X.X.....XXXX....X
 X....XX.XXX..XXX.X.....X.......X
 X..XXXX.X....X.X..XXXX.XXXX....X
 X..............................X
 X..XXXX..XX...XXXX.XXXX........X
 X..X..X.X..X.X.....XXXX........X
 X..XXX..XXXX.X.....X...........X
 X..X..X.X..X..XXXX.XXXX........X
 X..............................X
end

title
 COLUBK = $06
 COLUPF = $76
 drawscreen
 if joy0fire then goto skiptitle
 goto title
  
skiptitle
 pfclear
 ballx = 153
 bally = 86
 playfield:
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 X.X..........................X.X
 XXX..........................XXX
end

init
   a = 1
   b = 0
   c = 0
   d = 1 : rem 0 is nonactive, 1 is up, 2 is left, 3 is right
   q = 0
   r = 0
   score = 0
   scback = $A8
   player0x = 100
   player0y = 90
   ballheight = 2 : rem * Ball 2 pixels high.
   CTRLPF = $11 : rem * Ball 2 pixels wide.
   COLUPF = $1E : rem * Ball/playfield color.
   dim scback = $A4
   COLUP0 = $74
   COLUPF = $A8
   missile0height = 4
   scorecolor = 10
   
   dim scrollline = s
   scrollline = 0
   
   dim ballhitcount = k
   ballhitcount = 0
   
   goto Main

   
Main

   COLUBK = $A2
   b = (rand&3)
   if switchreset then init
   
   pfscroll down
   c = c + 1
   
   t = sread(mymusic)
   
   if missile0y>240 then goto skip 
   if missile0x < 45 || missile0x > 175 then goto skip
   if d = 0 then missile0x = player0x: missile0y=player0y
   if d = 1 then missile0y = missile0y - 1 : goto draw
   if d = 2 then missile0x = missile0x - 1 : goto draw
   if d = 3 then missile0x = missile0x + 1 : goto draw

skip
   missile0y=player0y-2:missile0x=player0x+4 : d = 0

draw   
   drawscreen
   if player0x > 140 then player0x = 139
   if player0x < 30 then player0x = 31
   
   if c = 8 then gosub playmove
   if joy0fire && m = 1 then missile0y=player0y:missile0x=player0x+(9/2) : missile0height = 4 : NUSIZ0=$10 : d = 1
   if joy0fire && m = 2 then missile0y=player0y-3:missile0x=player0x+(9/2) : missile0height = 1 : NUSIZ0=$20 : d = 2
   if joy0fire && m = 3 then missile0y=player0y-3:missile0x=player0x+(9/2) : missile0height = 1 : NUSIZ0=$20 : d = 3
   if joy0up then m = 1 : gosub changeup
   if joy0left then player0x = player0x - 1 :  m = 2 : REFP0 = 8 : gosub changeleft
   if joy0right then player0x = player0x + 1 :  m = 3 : REFP0 = 0 : gosub changeright
  
   if collision(player0, playfield) then reboot
   if collision(playfield, missile0) then  gosub deleteplayfield: missile0y=player0y-2:missile0x=player0x+4 : d = 0
   if collision(missile0, ball) then goto win
   
   goto Main

skipmove
   goto Main

changeleft
   player0:
 %01111111
 %11111111
 %11111100
 %01111000
 %00111000
 %00110000
 %00010000
 %00000000
end 
 return
   goto Main
changeright
   player0:
 %11111110
 %11111111
 %00111111
 %00011110
 %00011100
 %00001100
 %00001000
 %00000000
end 
 return 
changeup 
   player0:
 %00011000
 %00111100
 %11111111
 %01111110
 %00111100
 %00111100
 %00011000
 %00000000
end   
 return  
   
win 
 drawscreen
 playfield:
 X.X.X.XXX...XXX............X.X.X
 X.X.X..XX...XX.............X.X.X
 X.X.X..XX.X.XX.............X.X.X
 X.X.X...XXXXX..............X.X.X
 X.X.X.......XXXXXX.........X.X.X
 X.X.X.........XX...........X.X.X
 X.X.X.........XX...........X.X.X
 X.X.X.......XXXXXX.........X.X.X
 X.X.X............XXX..XX...X.X.X
 X.X.X............XXXX.XX...X.X.X
 X.X.X............XX.XXXX...X.X.X
 X.X.X............XX..XXX...X.X.X
 
end
 goto win
      
playmove
   c = 0
   i=4*(playlengths[0]-scrollline)
   var44=play1[i]
   i=i+1 
   var45=play1[i]
   i=i+1
   var46=play1[i]
   i=i+1
   var47=play1[i]
   scrollline = scrollline + 1 
   if scrollline > playlengths[0] then scrollline = 0
return

deleteplayfield
   q = (missile0x-32) /4
   r = missile0y/8
   if d = 1 && pfread(q,r) then pfpixel q r off 
 return

   asm
minikernel
   sta WSYNC
   lda scback
   sta COLUBK
   rts
end
